package com.juvepoland.jpdroid.managers.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.res.AssetManager;
import android.util.Log;

import com.juvepoland.jpdroid.common.JSONParser;
import com.juvepoland.jpdroid.managers.NewsManager;
import com.juvepoland.jpdroid.models.News;
import com.juvepoland.jpdroid.models.NewsResultType;

public class NewsManagerImpl implements NewsManager {
	
	static private final String URL_LIST_ALL = "http://jpdroid.juvepoland.com/get_all_news.php";
	static private final String URL_LIST_EVENTS = "http://jpdroid.juvepoland.com/get_events.php";
	static private final String URL_LIST_OTHERS = "http://jpdroid.juvepoland.com/get_other_news.php";
	static private final String URL_PREVIOUS_LIST_OTHERS = "http://jpdroid.juvepoland.com/get_previous_other_news.php";
	static private final String URL_GET_NEWS_DETAILS = "http://jpdroid.juvepoland.com/get_news_details.php"; 
	static private final String URL_INCREMENT_NEWS = "http://jpdroid.juvepoland.com/increment_news.php";
	static private final String CNT_NEWS = "http://jpdroid.juvepoland.com/get_news_cnt.php";
	
	private List<News> newsList = new ArrayList<News>();
	private int currentIndex=-1;
	
	@Override
	public List<News> getNewsList(NewsResultType result, int index, int limit) throws Exception{
		
		if(currentIndex==index){
			return newsList;
		}
		currentIndex=index;
		
		JSONParser jParser = new JSONParser();
		JSONArray jArray = new JSONArray();
		JSONObject jObject = null;
		//Log.d("INDEX", index+"");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//newsList = new ArrayList<News>();	//clear list
		
		
		params.add(new BasicNameValuePair("index", String.valueOf(index)));
		params.add(new BasicNameValuePair("limit", limit+""));
		
		
		switch (result) {
			case EVENTS:
			{
				jObject = jParser.makeHttpRequest(URL_LIST_EVENTS, "GET", params);
				break;
			}
			case OTHERS:
			{
				jObject = jParser.makeHttpRequest(URL_LIST_OTHERS, "GET", params);
				break;
			}
	
			default:
			{
				throw new Exception("News Result Type not supported!");
			}
		}
		//	Log.d("Result list: ", jsonObject.toString());
		int success = 0;
		success = jObject.getInt("success");
		if(success == 1){
			jArray = jObject.getJSONArray("news");

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jsonObject = jArray.getJSONObject(i);
				
				newsList.add(getNewsFromJSONObject(jsonObject, false));
				}
		}
		else{
			String message = jObject.getString("message");
			if(message.equalsIgnoreCase("No products found")){
				throw new Exception("Nie zwrócono więcej obiektów.");
			}
			else{
				throw new Exception(message);
			}
			
		}
		return newsList;
	}
	

	private List<News> getPreviousNews(NewsResultType type, int index) throws Exception{
		JSONParser jParser = new JSONParser();
		JSONArray jArray = new JSONArray();
		JSONObject jObject = null;
		
		List<News> list = new ArrayList<News>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("index", String.valueOf(index)));
		switch (type) {
			case EVENTS:
			{
				jObject = jParser.makeHttpRequest(URL_LIST_EVENTS, "GET", params);
				break;
			}
			case OTHERS:
			{
				jObject = jParser.makeHttpRequest(URL_PREVIOUS_LIST_OTHERS, "GET", params);
				break;
			}
	
			default:
			{
				throw new Exception("News Result Type not supported!");
			}
		}
		int success = 0;
		success = jObject.getInt("success");
		if(success == 1){
			jArray = jObject.getJSONArray("news");

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jsonObject = jArray.getJSONObject(i);
				
				list.add(getNewsFromJSONObject(jsonObject, false));
				}
		}
		else{
			throw new Exception("Blad");
		}
		return list;
	}
	
	@Override
	public News getNews(int id){
		JSONParser jParser = new JSONParser();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("pid", String.valueOf(id)));

		JSONObject jObject = jParser.makeHttpRequest(URL_GET_NEWS_DETAILS, "GET", params);
		
		int success = 0;
		try {
			success = jObject.getInt("success");
		} catch (JSONException e) {
				e.printStackTrace();
		}
		if (success == 1) {
			try {
				JSONArray productObj = jObject.getJSONArray("news");
				JSONObject c = productObj.getJSONObject(0);
					
				return getNewsFromJSONObject(c, true);
							
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
						
		}
		return null;
	}
	
	private News getNewsFromJSONObject(JSONObject jsonObject, boolean isSingleNewsView) throws Exception{
		News news = new News();
		news.setId(jsonObject.getInt(News.D_ID));
		if(isSingleNewsView){
			news.setContent(jsonObject.getString(News.D_CONTENT));
			news.setSource(jsonObject.getString(News.D_SOURCE));
			news.setCommentsCount(jsonObject.getInt(News.D_COMMENTS_COUNT));
		}
		
		news.setDate(jsonObject.getInt(News.D_DATE));
		news.setTitle(jsonObject.getString(News.D_TITLE));
		news.setCommentEnabled(jsonObject.getInt(News.D_COMMENTS_ENABLED));
		return news;
	}
	
	


	
}
